package coreservlets.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class LogFilter implements Filter {
	protected FilterConfig config;
	private ServletContext context;
	private String filterName;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		System.out.println(req.getRemoteHost() + " tried to access "
				+ req.getRequestURL() + " on " + System.currentTimeMillis()
				+ "." + "(Reported by " + filterName + ".)");
		chain.doFilter(req, response);
		System.out.println("this is log filter after chain.doFilter");
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		this.config = config;
		this.context = config.getServletContext();
		this.filterName = config.getFilterName();
	}

}
